<?php
namespace app\index\controller;
use think\Db;
use think\Controller;
use  think\Request;
use think\Csv; 
use think\Config;
use think\View;

class Home extends Base
{
    public function Index()
    {
        $issuper= session("issuper");
        $mid= session("mid");
       
        if($issuper=="1"){
            $where="textstr='超级管理'";
           
            $this->assign('appname', Config::get('appname'));

            $menu= Db('sys_menu')->where($where)->order('seq asc')->select();
            if($menu){
               $where="parentid=".$menu[0]["id"];
                $leftmenu= Db('sys_menu')->where($where)->order('id asc')->select();
               $this->assign('leftmenu', $leftmenu);
            }

        }else{
            $userid = session("userid");
            $manage = db("manage")->where("id=".$userid)->find();
            $jiaoseinfo = db("sys_jiaose")->where("id=".$manage["jiaoseid"])->find();
            $quanxian = $jiaoseinfo["quanxian"];


            $where="parentid=0 and textstr!='超级管理' and LOCATE(valuestr,'".$quanxian."')!=0";
            $mechanism= db("mechanism")->where("id=".$mid)->find();
            $endtime = date("Y-m-d",strtotime($mechanism["endtime"])); 
            if($mid>0){
                $this->assign('appname', $mechanism["name"]."  到期时间:".$endtime);
            }

         
            $menu= Db('sys_menu')->where($where)->order('seq asc')->select();
            if(count($menu)>0){
               
                $where="parentid=".$menu[0]['id']." and LOCATE(valuestr,'".$quanxian."')!=0";
                $leftmenu= Db('sys_menu')->where($where)->order('id asc')->select();
                $this->assign('leftmenu', $leftmenu);
            }
    
        }
		
		
      

        
        // 模板变量赋值
        $this->assign('username', session("username"));
        $this->assign('menu', $menu);
		return view();
    }
	
	public function Home()
    {
     
        $issuper= session("issuper");
        $dailiid= session("dailiid");
       
        $view = new View();
        if($issuper=="1"){
            $now = date("Y-m-d");
            $allnum = db("mechanism")->where("state=1")->count();
            $daoqinum = db("mechanism")->where("endtime < '".$now."' and state=1")->count();
            $newaddnum = db("mechanism")->where("DATE_FORMAT(jointime,'%Y%m')=DATE_FORMAT(CURDATE(),'%Y%m') and state=1")->count();
            $monthdaoqinum = db("mechanism")->where("DATE_FORMAT(endtime,'%Y%m')=DATE_FORMAT(CURDATE(),'%Y%m') and state=1")->count();
            $usercount = db("users")->where("")->count();

            $info["allnum"]=$allnum;
            $info["daoqinum"]=$daoqinum;
            $info["newaddnum"]=$newaddnum;
            $info["monthdaoqinum"]=$monthdaoqinum;
            $info["usercount"]=$usercount;
            
            
            return $view->fetch('home',$info); 
        }else if($dailiid){
           
            $info = db("daili")->where("id=".$dailiid)->find();
         
       
            $info["num"] = db("mechanism")->where("dailiid=".$dailiid)->count();

            return $view->fetch('dailihome',$info); 
        }

		
    }
	
	 public function upload(){
		 
        // 获取表单上传的文件，例如上传了一张图片
        $file = request()->file('imgFile');
        if($file){
            //将传入的图片移动到框架应用根目录/public/uploads/ 目录下，ROOT_PATH是根目录下，DS是代表斜杠 / 
            $info = $file->move(ROOT_PATH . 'public' . DS . 'static'. DS .'uploads');
            if($info){
                return $info->getSaveName();
            }else{
                // 上传失败获取错误信息
                echo $file->getError();die;
            }
        }
		
     }
     public function config(){
        $mid =  session("mid");


      
        $request = Request::instance();
        if($request->isPost()){
          
            $data["wucantime"]=$request->post('wucantime');
            $data["signnum"]=$request->post('signnum');
            $data["signjgtime"]=$request->post('signjgtime');

            if(db('sys_webconfig')->where('mid',$mid)->update($data)){
					
                return ['status'=>200,'message'=>'修改成功'];
          }else{
               return ['status'=>500,'message'=>'修改失败'];
          }
        }else{
            $mm = db("mechanism")->where("id=".$mid)->find();
            $this->assign('mm',$mm);


            $info = db("sys_webconfig")->where("mid=".$mid)->find();
            if($info){
                $this->assign('info',$info);
            }else{
                $data["mid"]=$mid;
                db('sys_webconfig')->insertGetId($data);


            }
           
            return view();

        }
      
     }
     public function import(){


         return view();
     }
     public function importstudent(){

        return view();
     }
     public function loadteachercsv(){
      
        $request = Request::instance();
        $filename=ROOT_PATH."public/static/uploads/".$request->post("filename");
        $handle = fopen($filename,'r');
        $csv = new Csv();
        $result = $csv->input_csv($handle); // 解析csv
        $len_result = count($result);
        if($len_result == 0){
          
            // return ['status'=>400,'message'=>'此文件中没有数据！'];
        }
        $items= array();
        for($i = 1;$i < $len_result+1;$i++)
        {
            $arr = array_values($result[$i]);
            $data['teachername'] = iconv('gb2312','utf-8',$arr[0] ); 
            $data['phone'] = iconv('gb2312','utf-8',$arr[1] ); 
            $data['sex'] = iconv('gb2312','utf-8',$arr[2] ); 
            $data['idcard'] = iconv('gb2312','utf-8',$arr[3] ); 
            $data['overschool']= iconv('gb2312','utf-8',$arr[4] ); 
            $data['jointime']=  iconv('gb2312','utf-8',$arr[5] ); 
            $data['outtime']=  iconv('gb2312','utf-8',$arr[6] ); 
            $data['otherphone']= iconv('gb2312','utf-8',$arr[7] ); 
            array_push($items,$data);
           
        }
       fclose($handle);
       return ['status'=>200,'data'=>$items];
     }
     public function loadstudentcsv(){
        $request = Request::instance();
        $mid= session("mid");
        $filename=ROOT_PATH."public/static/uploads/".$request->post("filename");
        $handle = fopen($filename,'r');
        $csv = new Csv();
        $result = $csv->input_csv($handle); // 解析csv
        $len_result = count($result);
        if($len_result == 0){
          
            // return ['status'=>400,'message'=>'此文件中没有数据！'];
        }
        $items= array();
        for($i = 1;$i < $len_result+1;$i++)
        {
            $arr = array_values($result[$i]);
            $data['number'] = iconv('gb2312','utf-8',$arr[0] ); 
            $data['truename'] = iconv('gb2312','utf-8',$arr[1] ); 
            $data['sex']= iconv('gb2312','utf-8',$arr[2] ); 
            $data['classname']= iconv('gb2312','utf-8',$arr[3] ); 
          
            $classinfo = db("myclass")->where("classname='".$data["classname"]."' and mid=".$mid)->find();
            if($classinfo){
             
                $data['classid'] = $classinfo["id"];
            }else{
                $data['classname'] = "不存在的班级";
                $data['classid'] = "0";
            }


            $data['fathername'] = iconv('gb2312','utf-8',$arr[4] ); 
            $data['fatherphone']= iconv('gb2312','utf-8',$arr[5] ); 
            $data['mothername']=  iconv('gb2312','utf-8',$arr[6] ); 
            $data['motherphone']=  iconv('gb2312','utf-8',$arr[7] ); 
            $data['otherphone']= iconv('gb2312','utf-8',$arr[8] ); 
            array_push($items,$data);
           
        }
       fclose($handle);
       return ['status'=>200,'data'=>$items];
     }


     public function updatepass(){
        $userid =  session("userid");

        $request = Request::instance();
        if($request->isPost()){
            $pass1=$request->post('pass1');
            $pass2=$request->post('pass2');
            if($pass1!=$pass2){
                return ['status'=>500,'message'=>"两次输入密码不一致"];
            }else{
                $updateresult = Db::execute('update manage set password=? where id=?',[md5($pass1),$userid]);
                return ['status'=>200,'message'=>"密码修改成功"];
            }
        }else{

            return view();

        }
       
    }
    public function manage(){

        return view();
    }

    
    public function manageadd(){
		
		$request = Request::instance();
	
		
		if($request->isPost()){
				$data["id"] =$request->post('id');
				$data["username"] =$request->post('username');
				$data["state"] =$request->post('state');
				$data["jiaoseid"] =$request->post('jiaoseid');
				$data["mid"] =session("mid");
				$data["createtime"] =  date("Y-m-d H:i:sa");
				$data["type"] ="0";
			
				
			if($data["id"]){
				
			
				
				if(db('manage')->update($data)){
					
					  return ['status'=>200,'message'=>'修改成功'];
				}else{
					 return ['status'=>500,'message'=>'修改失败'];
				}
			}else{
              $data["password"] =  md5($request->post('password'));
              
                $manage = db("manage")->where("username='".$data["username"]."'")->find();
                if($manage){
                    return ['status'=>500,'message'=>'用户名重复'];
                }else{
                    if(db('manage')->insertGetId($data)){
                        return ['status'=>200,'message'=>'添加成功'];
                       
                   }else{
                       return ['status'=>500,'message'=>'添加失败'];
                   }
                }

			
			}
			
		}else{
			
			if($request->get('id')){
				$info =db('manage')->where("id=".$_GET["id"])->find();
				$this->assign('info',$info);
				
			}
			
			
		
		
	
		
			
		}
		
		
	
	
		return view();
	}
	 public function getmanage(){
		$where="mid=".session("mid");
		$count =db('manage')->where($where)->count();
		$first = $_GET["size"] * ($_GET["page"] -1);
		 
		$list = db('manage')->where($where)->limit($first,$_GET["size"])->select();
		foreach ($list as $key => &$value) {
		    $jiaoseinfo = db('sys_jiaose')->where("id=".$value["jiaoseid"])->find();
			$value["jiaosename"]= $jiaoseinfo["jiaosename"];
			
		}
		
		$obj["count"]=(int)($count/$_GET["size"])+1;
		$obj["allcount"]=$count;
		$obj["list"]=$list;
		return json($obj);
		
	}
	
	function managedel(){
		$request = Request::instance();
		$id =$request->post('id');

		 
			$where = 'id='.$id;
		  
		   $list=db('manage')->where($where)->delete();
		   if($list!==false) {
			 return ['status'=>200,'message'=>'删除成功'];
		   }else{
			  return ['status'=>500,'message'=>'删除失败'];
		   }
		
	}


	
}
